package com.buba.personnel.dao.impl;

import com.buba.personnel.dao.administerDao;
import com.buba.personnel.pojo.Pagination;
import com.buba.personnel.pojo.admin;
import com.buba.util.DBPool;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class administerDaoImpl implements administerDao {
    QueryRunner queryRunner = new QueryRunner(DBPool.getDS());

    /**
     * 查询总条数
     * @param pagination
     * @return
     */
    @Override
    public long selCount(Pagination<admin> pagination) {
        String sql = "select count(*) from userinfo a left join departinfo b on  a.DepartId=b.DepartId left join RoleInfo c on a.RoleId=c.RoleId where 1=1 ";

        String sql2 = condition(sql, pagination.getParam());
        // 查询总条数时没有分页
        try {
            return queryRunner.query(sql2, new ScalarHandler<>());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 员工查询方法
     * @param pagination 工具类
     * @return
     */
    @Override
    public List<admin> selAdmin(Pagination<admin> pagination) {
        String sql = "select * from userinfo a left join departinfo b on  a.DepartId=b.DepartId left join RoleInfo c on a.RoleId=c.RoleId where 1=1 ";
        String sql2 = condition(sql, pagination.getParam());
        // 添加分页 省略了计算起始下标的步骤，因为在Pagination工具类已经计算完了
        sql2 += " limit " + pagination.getIndex() + "," + pagination.getLimit();
        try {
            return queryRunner.query(sql2,new BeanListHandler<>(admin.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    /**
     * 添加
     * @param param
     */
    @Override
    public void insert(Map<String, String> param) {
        String sql = "insert into userinfo values(null,?,?,?,?,?,?,?)";
        Object[] objects = new Object[7];
        objects[0] = param.get("userName");
        objects[1] = param.get("password");
        objects[2] = param.get("sellist");
        objects[3] = param.get("optradio");
        objects[4] = param.get("RoleId");
        objects[5] = param.get("UserState");
        objects[6] = param.get("upFile");
        try {
            queryRunner.update(sql,objects);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 修改
     * @param param
     * @return
     */
    @Override
    public int updAdmin(Map<String, String> param) {
        //编写sql
        String sql = "update userinfo set UserName=?,PassWord=?,DepartId=?,Gender=?,RoleId=?,UserState=?,headImg=? where UserId=?";
        //给占位符赋值
        Object[] objects = new Object[8];
        objects[0] = param.get("userName");
        objects[1] = param.get("passWord");
        objects[2] = param.get("departId");
        objects[3] = param.get("gender");
        objects[4] = param.get("roleId");
        objects[5] = param.get("userState");
        objects[6] = param.get("headImg");
        objects[7] = param.get("userId");
        try {
            return queryRunner.update(sql,objects);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    /**
     * 员工删除
     * @param param
     * @return
     */
    @Override
    public int delAdmin(Map<String, String> param) {
        String sql = "delete from userinfo where UserId="+param.get("userId");
        try {
            return queryRunner.update(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    /**
     * 拼接员工的查询条件
     * @param sql
     * @param param
     * @return
     */
    private String condition(String sql, Map<String, String> param) {
        String userName = param.get("UserName");
        String departName = param.get("DepartName");
        if (userName != null && !userName.trim().equals("")){
            sql += " and userName like'" + userName + "%'";
        }
        if (departName != null && !departName.trim().equals("")){
            sql += " and departName like'" + departName + "%'";
        }
        return sql;
    }
}
